﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace QLCH_ANUONG
{
    public partial class frmMain : Form
    {
        private bool booDangNhap = false;
        public frmMain()
        {
            InitializeComponent();
        }
        private void DangNhap()
        {
            try
            {
                if (booDangNhap == false)
                {
                    DangNhapToolStripMenuItem.Text = "Đăng xuất";
                }
                else
                {
                    booDangNhap = false;
                    clsMain.booLogin = false;
                    DangNhapToolStripMenuItem.Text = "Đăng nhập";
                    trvMenu.Nodes.Clear();
                }

                Form frm = new Forms.frmDangNhap();
                frm.ShowDialog();

                if (clsMain.booLogin == true)
                {
                    ConnectSystem();
                    booDangNhap = true;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Dang nhap Function\n" + ex.ToString());
            }
        }
        private void ConnectSystem()
        {
            try
            {
                string strSQLParent = "";
                string strSQLChild = "";
                strSQLParent = "select Code,Data,IconID from System_Menus";
                strSQLChild = "select Code,Data,IconID,Proc_ListView,Proc_SaveData,Proc_tags from System_Childs where Parent_Code = ";
                strSQLChild = "select Code,Data,IconID,Proc_ListView,Proc_SaveData,Proc_tags from System_Childs, QuyenNguoiDung where  QuyenNguoiDung.USER_CODE='" + clsMain.strUserLogin + "' and System_Childs.Code=QuyenNguoiDung.SYSCHILD_CODE and System_Childs.Parent_Code = ";
                clsMain cls = new clsMain();
                cls.AddDataToTreeView(strSQLParent, strSQLChild, trvMenu);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Connect to system\n" + ex.ToString());
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            clsMain.flexConn.DatabaseConnection(false);
            if (clsMain.flexConn.fcConn != null)
            {
                DangNhap();
                Form frm = new Forms.frmNhanVien();
                KhoiTaoForms(frm);
                frm = new Forms.frmTinhThanh();
                KhoiTaoForms(frm);
                frm = new Forms.frmNguoiDung();
                KhoiTaoForms(frm);
                frm = new Forms.frmCaiDatQuyen();
                KhoiTaoForms(frm);
                frm = new Forms.frmLoaiNV();
                KhoiTaoForms(frm);
                frm = new Forms.frmCaLamViec();
                KhoiTaoForms(frm);
                frm = new Forms.frmNVHistory();
                KhoiTaoForms(frm);
                frm = new Forms.frmLichLamViec();
                KhoiTaoForms(frm);
                frm = new Forms.frmDinhMuc();
                KhoiTaoForms(frm);
                frm = new Forms.frmBaoCaoThang();
                KhoiTaoForms(frm);
                frm = new Forms.frmReport();
                KhoiTaoForms(frm);
                frm = new Forms.frmInBaoCao();
                KhoiTaoForms(frm);
            }
            
        }
        private void KhoiTaoForms(System.Windows.Forms.Form frm)
        {
            try
            {

               // frm.MdiParent = this;
                //Khởi tạo form
                clsMain.flexConn.InitForms(ref frm, clsMain.flexConn.fcConn);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Khởi tạo forms\n" + ex.ToString());
            }
        }

        private void trvMenu_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (trvMenu.SelectedNode.Tag != null)
                {
                    string strTag = Convert.ToString(trvMenu.SelectedNode.Tag);

                    int intIDForm = Convert.ToInt32(strTag);

                    if (clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].booActive == true)
                    {
                        int i = 0;
                        bool isShowFlag = false;
                        for (; i < xtraTabControl.TabPages.Count; i++)
                        {
                            if (xtraTabControl.TabPages[i].Tag == clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Tag)
                            {
                                if (xtraTabControl.TabPages[i].PageVisible == false)
                                    xtraTabControl.TabPages[i].PageVisible = true;
                                xtraTabControl.SelectedTabPage = xtraTabControl.TabPages[i];

                                isShowFlag = true;
                            }
                        }
                        if (isShowFlag == false)
                        {
                            if (intIDForm != 13)//Code tạm
                            {
                                //Forms_QLHS.fcForms[intIDForm].fcForm.Load;
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].booIsShow = true;
                                clsMain.flexConn.LoadListView(intIDForm);
                                clsMain.flexConn.GenerateCode(intIDForm);
                                clsMain.flexConn.ClearTextControll(intIDForm);
                                //Forms_QLHS.fcForms[intIDForm].fcForm.Visible=true;
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.TopLevel = false;

                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Visible = true;
                                // clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.FormBorderStyle = FormBorderStyle.None;
                                //clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Dock = DockStyle.Fill;
                                xtraTabControl.TabPages.Add();
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
                                xtraTabControl.TabPages[xtraTabControl.TabPages.Count - 1].Controls.Add(clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm);
                                xtraTabControl.TabPages[xtraTabControl.TabPages.Count - 1].BackColor = System.Drawing.Color.Blue;
                                xtraTabControl.TabPages[xtraTabControl.TabPages.Count - 1].Tag = clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Tag;
                                xtraTabControl.SelectedTabPage = xtraTabControl.TabPages[xtraTabControl.TabPages.Count - 1];
                                
                                xtraTabControl.TabPages[xtraTabControl.TabPages.Count - 1].Text = clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Text;
                                xtraTabControl.TabPages[xtraTabControl.TabPages.Count - 1].ImageIndex = trvMenu.SelectedNode.ImageIndex;
                                
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Show();
                                
                                toolStripStatusLabel2.Text = clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Text;
                            }
                            else if(intIDForm==13)//Code tạm
                            {
                                xtreportDANHSACHNV xtr = new xtreportDANHSACHNV();
                                xtr.ShowPreview();
                            }
                        }
                    }


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("treeview menu doubleclick\n" + ex.ToString());
            }
        }
        public void closeTab()
        {
           
        }
        private void xtraTabControl_CloseButtonClick(object sender, EventArgs e)
        {

            DevExpress.XtraTab.XtraTabControl tabControl = sender as DevExpress.XtraTab.XtraTabControl;
            DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs arg = e as DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs;
          //  (arg.Page as DevExpress.XtraTab.XtraTabPage ).PageVisible = false;
            xtraTabControl.TabPages.Remove((arg.Page as DevExpress.XtraTab.XtraTabPage));
        }

        private void frmMain_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void đăngNhậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DangNhap();
        }

        private void giớiThiệuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Forms.frmAbout frm =new Forms.frmAbout();
            xtraTabControl.TabPages.Add();
            //frm.TopLevel = false;
            frm.Visible = true;

            //xtraTabControl.TabPages[xtraTabControl.TabPages.Count - 1].Controls.Add(frm);
            frm.Show();
        }

        private bool booMenuShow = true;
        private void butMenuShow_Click(object sender, EventArgs e)
        {
            if (booMenuShow == false)
            {
                trvMenu.Visible = true;
                booMenuShow = true;
                xtraTabControl.Location = new Point(360,24);
                xtraTabControl.Size = new Size(xtraTabControl.Size.Width - 313, xtraTabControl.Size.Height);
                butMenuShow.ImageIndex = 18;
            }
            else
            {
                xtraTabControl.Location = new Point(45, 24);
                xtraTabControl.Size=new Size(xtraTabControl.Size.Width+313,xtraTabControl.Size.Height);
                trvMenu.Visible = false;
                booMenuShow = false;
                butMenuShow.ImageIndex = 19;
            }

        }

        private void càiĐặtHệThốngToolStripMenuItem_Click(object sender, EventArgs e)
        {
               clsMain.flexConn.DatabaseConnection(true);
            DangNhap();
            
        }

        

        
    }
}
